package com.sl.crawler.robot.clientPool;

import com.sl.crawler.robot.request.Request;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by shiling on 2017/5/26.
 */
public class SeleniumWebDriverPool extends BaseClientPool<WebDriver> {
    private final static Logger LOG = LoggerFactory.getLogger(SeleniumWebDriverPool.class);

    @Override
    public void closeOne(WebDriver client) {
        client.close();
    }

    @Override
    public WebDriver createClient() {
        DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
        capabilities.setJavascriptEnabled(true);
        capabilities.setJavascriptEnabled(true);
        WebDriver newWebDriver = new HtmlUnitDriver(capabilities);
        return newWebDriver;
    }

    @Override
    public String getUrl(WebDriver webDriver, Request request) throws Exception {
        webDriver.get(request.getUrl());
        String pageSource = webDriver.getPageSource();
        return pageSource;
    }

}
